package cn.rhzz.restore;

import org.apache.commons.io.FileUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

public class ConfigFile {

	private JSONObject jsonObject = null;
	private static final Logger logger = LoggerFactory.getLogger(ConfigFile.class);

	public ConfigFile(String filepath) throws IOException {
		if (filepath == null || "".equals(filepath.trim())) {
			logger.error("Error: cannot find the config file, please give the config file path as paramter ConfigFile()");
			throw new IOException("Error: cannot find the config file, please give the config file path as paramter");
		}

		File file = new File(filepath);
		if (!file.exists()) {
			logger.error("Error: ConfigFile() Config file doesn't exist in path: " + filepath);
			throw new IOException("Error: Config file doesn't exist in path: " + filepath);
		}

		logger.info("Config file: " + file.getAbsolutePath());
		String content = null;
		try {
			content = FileUtils.readFileToString(file, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.jsonObject = new JSONObject(content);

	}

	public String getValue(String key) {
		return this.jsonObject.getString(key);
	}

	public int getIntValue(String key) {
		return this.jsonObject.getInt(key);
	}


}
